package com.wpen.unit.service.job.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wpen.unit.cvt.job.AgentExec1LogCvt;
import com.wpen.unit.cvt.job.JobExec1LogCvt;
import com.wpen.unit.exception.BizException;
import com.wpen.unit.infra.mybatis.job.po.AgentExec1LogPo;
import com.wpen.unit.infra.mybatis.job.po.JobExec1LogPo;
import com.wpen.unit.infra.mybatis.job.po.JobExec1RedologPo;
import com.wpen.unit.infra.repository.job.AgentExec1LogRepository;
import com.wpen.unit.infra.repository.job.JobExec1LogRepository;
import com.wpen.unit.infra.repository.job.JobExec1RedologRepository;
import com.wpen.unit.service.job.JobExec1LogService;
import com.wpen.unit.utils.DateUtil;
import com.wpen.unit.web.dto.job.JobExec1LogDetailRequest;
import com.wpen.unit.web.dto.job.JobExec1LogDetailResponse;
import com.wpen.unit.web.dto.job.JobExec1LogListRequest;
import com.wpen.unit.web.dto.job.JobExec1LogListResponse;
import com.wpen.unit.web.dto.job.JobExec1LogListRowsResponse;
import com.wpen.unit.web.dto.job.JobExec1RedologListRequest;
import com.wpen.unit.web.dto.job.JobExec1RedologListResponse;
import com.wpen.unit.web.dto.job.JobExec1RedologListRowsResponse;
import com.wpen.unit.web.dto.job.JobExecAgentLogListRequest;
import com.wpen.unit.web.dto.job.JobExecAgentLogListResponse;
import com.wpen.unit.web.dto.job.JobExecAgentLogListRowsResponse;
import com.wpen.unit.web.dto.job.StatRequest;
import com.wpen.unit.web.dto.job.StatResponse;

/**
 * 任务作业执行1日志业务处理.
 * 
 * @author Wang Peng
 * @date 2022-12-06
 */
@Service
public class JobExec1LogServiceImpl implements JobExec1LogService {

	@Autowired
	private JobExec1LogRepository jobExec1LogRepository;
	@Autowired
	private JobExec1RedologRepository jobExec1RedologRepository;
	@Autowired
	private AgentExec1LogRepository agentExec1LogRepository;

	@Override
	public StatResponse statTodayJobExec(StatRequest statRequest) {
		StatResponse statResponse = new StatResponse();
		// 数据库查询
		JobExec1LogPo jobExec1LogPo = new JobExec1LogPo();
		jobExec1LogPo.setExecDate(DateUtil.format(DateUtil.FORMAT_DATE2));
		long total = jobExec1LogRepository.count(jobExec1LogPo);
		statResponse.setCount(total);
		jobExec1LogPo.setExecDate(DateUtil.offset(Calendar.DAY_OF_YEAR, -1, DateUtil.FORMAT_DATE2));
		long total2 = jobExec1LogRepository.count(jobExec1LogPo);
		statResponse.setAlive(total2);
		return statResponse;
	}

	@Override
	public JobExec1LogListResponse queryListJobExec1Log(JobExec1LogListRequest jobExec1LogListRequest) {
		int index = jobExec1LogListRequest.getIndex();
		int size = jobExec1LogListRequest.getSize();
		// 数据库查询
		JobExec1LogPo jobExec1LogPo = JobExec1LogCvt.INSTANCE.queryListDto2Do(jobExec1LogListRequest);

		long total = jobExec1LogRepository.count(jobExec1LogPo);
		List<JobExec1LogPo> jobExec1LogPoLists = jobExec1LogRepository.queryList(index, size, jobExec1LogPo);

		// 返回响应信息
		JobExec1LogListResponse jobExec1LogListResponse = new JobExec1LogListResponse();
		jobExec1LogListResponse.setTotal(total);
		jobExec1LogListResponse.setCount(0);
		if (jobExec1LogPoLists != null) {
			jobExec1LogListResponse.setCount(jobExec1LogPoLists.size());
			List<JobExec1LogListRowsResponse> listRows = jobExec1LogListResponse.getRows();
			for (JobExec1LogPo inJobExec1LogPo : jobExec1LogPoLists) {
				JobExec1LogListRowsResponse row = JobExec1LogCvt.INSTANCE.queryListRowDo2Dto(inJobExec1LogPo);
				listRows.add(row);
			}
			jobExec1LogListResponse.setRows(listRows);
		}

		return jobExec1LogListResponse;
	}

	@Override
	public JobExec1RedologListResponse queryListJobExec1Redolog(JobExec1RedologListRequest jobExec1RedologListRequest) {
		int index = jobExec1RedologListRequest.getIndex();
		int size = jobExec1RedologListRequest.getSize();
		// 数据库查询
		JobExec1RedologPo jobExec1RedologPo = JobExec1LogCvt.INSTANCE.queryListDto2Do(jobExec1RedologListRequest);

		List<JobExec1RedologPo> jobExec1RedologPoLists = jobExec1RedologRepository.queryList(index, size,
				jobExec1RedologPo);

		// 返回响应信息
		JobExec1RedologListResponse jobExec1RedologListResponse = new JobExec1RedologListResponse();
		jobExec1RedologListResponse.setTotal(0);
		jobExec1RedologListResponse.setCount(0);
		if (jobExec1RedologPoLists != null) {
			jobExec1RedologListResponse.setTotal(jobExec1RedologPoLists.size());
			jobExec1RedologListResponse.setCount(jobExec1RedologPoLists.size());
			List<JobExec1RedologListRowsResponse> listRows = jobExec1RedologListResponse.getRows();
			for (JobExec1RedologPo inJobExec1RedologPo : jobExec1RedologPoLists) {
				JobExec1RedologListRowsResponse row = JobExec1LogCvt.INSTANCE.queryListRowDo2Dto(inJobExec1RedologPo);
				listRows.add(row);
			}
			jobExec1RedologListResponse.setRows(listRows);
		}

		return jobExec1RedologListResponse;
	}

	@Override
	public JobExec1LogDetailResponse detailJobExec1Log(JobExec1LogDetailRequest jobExec1LogDetailRequest) {
		Long id = jobExec1LogDetailRequest.getId();
		Long triggerTime = jobExec1LogDetailRequest.getTaskTriggerTime();
		// 数据库查询
		JobExec1LogPo jobExec1LogPoDb = null;
		if (triggerTime == null) {
			jobExec1LogPoDb = jobExec1LogRepository.queryById(id);
			if (jobExec1LogPoDb == null) {
				throw new BizException("E00001", jobExec1LogDetailRequest.getId() + "");
			}
		} else {
			jobExec1LogPoDb = jobExec1LogRepository.queryByIdAndTime(id, triggerTime);
			if (jobExec1LogPoDb == null) {
				JobExec1RedologPo jobExec1RedologPo = jobExec1RedologRepository.queryByJobLogId(id, triggerTime);
				jobExec1LogPoDb = JobExec1LogCvt.INSTANCE.jobRedoLog2Log(jobExec1RedologPo);
			}
		}

		// 返回响应信息
		JobExec1LogDetailResponse jobExec1LogDetailResponse = JobExec1LogCvt.INSTANCE.detailDo2Dto(jobExec1LogPoDb);
		return jobExec1LogDetailResponse;
	}

	@Override
	public JobExecAgentLogListResponse queryListExecAgentLog(JobExecAgentLogListRequest jobExecAgentLogListRequest) {
		// 查询执行器 为主，job为辅的列表
		Long batchLogId = jobExecAgentLogListRequest.getBatchExecLogId();
		Long batchTrigger = jobExecAgentLogListRequest.getBatchTriggerTime();
		String execStatus = jobExecAgentLogListRequest.getExecStatus();

		List<JobExec1LogPo> jobExec1LogPos = jobExec1LogRepository.queryListByBatchId(batchLogId, batchTrigger);
		if (jobExec1LogPos == null || jobExec1LogPos.isEmpty()) {
			return new JobExecAgentLogListResponse();
		}
		Map<Long, JobExec1LogPo> jobExecLogMaps = new HashMap<>();
		Set<Long> taskLogIdSets = new HashSet<>();
		jobExec1LogPos.forEach(item -> {
			taskLogIdSets.add(item.getTaskExecLogId());
			jobExecLogMaps.put(item.getId(), item);
		});
		List<Long> taskLogIds = new ArrayList<>();
		taskLogIdSets.forEach(item -> {
			taskLogIds.add(item);
		});
		List<AgentExec1LogPo> agentExec1LogPos = agentExec1LogRepository.queryListByBatchLogId(taskLogIds, execStatus);

		JobExecAgentLogListResponse jobExecAgentLogListResponse = new JobExecAgentLogListResponse();
		List<JobExecAgentLogListRowsResponse> rows = jobExecAgentLogListResponse.getRows();
		for (AgentExec1LogPo agentExec1LogPo : agentExec1LogPos) {
			JobExecAgentLogListRowsResponse row = AgentExec1LogCvt.INSTANCE.queryListRowDo2Dto2(agentExec1LogPo);
			JobExec1LogPo jobExec1LogPo = jobExecLogMaps.get(agentExec1LogPo.getJobExecLogId());
			row.setBatchCode(jobExec1LogPo.getBatchCode());
			row.setTaskCode(jobExec1LogPo.getTaskCode());
			row.setJobCode(jobExec1LogPo.getJobCode());
			row.setBatchTriggerTime(jobExec1LogPo.getBatchTriggerTime());
			row.setExecTime(DateUtil.execTime(agentExec1LogPo.getStartupTime(), agentExec1LogPo.getEndTime()));
			rows.add(row);
		}
		return jobExecAgentLogListResponse;
	}

}
